Ext.define('Holo.controller.ManagerSpot', {
    extend: 'Ext.app.Controller',
	
    views: [
        'ManagerSpot',
        'spot.FormCreateSpotDesc',
        'spot.FormCreateSpotDescContent',
        'spot.ListSpotDesc',
	'spot.ListSpotDescContent'
    ],
    
    stores: [
        'Spots'
    ],
    
    
    refs: [
	{
	   selector: '#SpotDescriptionsLabelForm',
	   ref: 'formAddLabel'
	},
        {
	   selector: '#SpotDescriptionsContentForm',
	   ref: 'formAddContent'
	},
        {
	   selector: '#SpotDescriptionsContentForm htmleditor',
	   ref: 'formContentHtmleditor'
	},
	{
	   selector: '#SpotDescriptionsContentForm textareafield',
	   ref: 'formContentShortDesc'
	},
        {
	   selector: '#SpotDescriptionsContentForm filefield',
	   ref: 'formContentFilefield'
	}
        
        
    ],


    
    init: function() {
    	 this.control({
        
            'listspotdesc': {
                afterrender : function(cmp){
		    //load the store ('SpotDescriptionsLabel') affected when we create the cmp
                    cmp.getStore().load();
                }
            },
            
	    
	            
            'listspotdesccontent': {
                afterrender : function(cmp){
		    //load the store ('SpotDescriptionsContent') affected when we create the cmp
                    cmp.getStore().load();
                }
            },
	    
            'createspotdesccontent': {
                afterrender : function(cmp){
		    //set the hiiden field with current spot textid to submit the value and create a folder with it
		    cmp.child('hiddenfield[name=idtextspot]').setValue(GlobalData.currentSpot.get('id_text'));
		}
            },
	
	   
	   	    
	    'createspotdesc button[action=createspotdesc]': {
                click : this.createSpotDescriptionTypeLabel
            
            },
	     
	    
	    'createspotdesccontent button[action=createspotdesccontent]': {
                click : this.createSpotDescriptionTypeContent
            
            },
	    
	    
	    

            'createspotdesccontent radiogroup radiofield': {
                change : function( field,  newValue,  oldValue, eOpts){
                    //Show/Hide htmleditor according to choose of the user
                    if(newValue){
                        var fileInput = this.getFormContentFilefield();
                        var htmlInput  = this.getFormContentHtmleditor();
			var shorDesc = this.getFormContentShortDesc();
                        if (field.getId() == "typeT"){
                            fileInput.hide();
                            fileInput.disable();
			    shorDesc.hide()
			    shorDesc.disable()
			    htmlInput.show();
                            htmlInput.enable();
                        }else{
                            fileInput.show();
                            fileInput.enable();
			    shorDesc.show();
                            shorDesc.enable();
                            htmlInput.hide();
                            htmlInput.disable();
                        }
                    }
                }
            
            }
            
        
        });
    },
    

    /**
     * Submit the forme. Add a Spot description type Label to the SpotDescriptionsLabel
     */
    createSpotDescriptionTypeLabel : function(){
        var newDescriptionData = this.getFormAddLabel().getValues();
        //console.log(newDescriptionData);
        try{
                var newSpotDescription = Ext.create('Holo.model.SpotDescription', {
                                            spot_id: GlobalData.currentSpot.get('id'),
                                            type : 'L',
                                            language: newDescriptionData.codelang,
                                            title: newDescriptionData.title,
                                            text: newDescriptionData.description
                                        });
                
               // console.log(newSpotDescription);
		Ext.data.StoreManager.lookup('SpotDescriptionsLabel').add(newSpotDescription);
                this.getFormAddLabel().getForm().reset();
                return true;
                
        }catch(err){
                console.log('erreur : controller/ManagerSpot.createSpotDescriptionTypeLabel() =>'+ err.message);
                return false;
        }
    },
    
    /**
     *
     */
    createSpotDescriptionTypeContent : function(){
	var form = this.getFormAddContent().getValues();
	
	if(form.type!='T'){
	    //get extension
	    var fileName =this.getFormAddContent('form').child('filefield[name=file]').getSubmitValue();
	    var tabName = fileName.split('.');
	    var ext = tabName[tabName.length-1];
	    
	    var is_goodExt = false;
	    switch(form.type){
		case "P":
		    is_goodExt = ((ext == 'jpg')||(ext == 'jpeg'));
		break;
		case "V":
		    is_goodExt = (ext == 'avi');
		break;
	    
		case "A":
		    is_goodExt = (ext == 'mp3');
		break
	    }
	
	    if(is_goodExt){
		
		this.getFormAddContent().submit({
                    url: 'database/processFile.php',
                    waitMsg: 'Uploading file...',
                    
		    success: function(fp, o) {
			var newSpotDescription = Ext.create('Holo.model.SpotDescription', {
				    spot_id: GlobalData.currentSpot.get('id'),
				    type : form.type,
				    language: form.codelang,
				    title: form.title,
				    text: form.description
				});

			Ext.data.StoreManager.lookup('SpotDescriptionsContent').add(newSpotDescription);
                    },
		    
		    failure: function(form, action) {
			console.log(action);
			switch (action.failureType) {
			    case Ext.form.action.Action.CLIENT_INVALID:
				Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
				break;
			    case Ext.form.action.Action.CONNECT_FAILURE:
				Ext.Msg.alert('Failure', 'Ajax communication failed');
				break;
			    case Ext.form.action.Action.SERVER_INVALID:
			       Ext.Msg.alert('Failure', action.result.msg);
		       }
		    }
                });

	    }else{
		//wrong extension...
	    }
	    
	}else{

	    var newSpotDescription = Ext.create('Holo.model.SpotDescription', {
						spot_id: GlobalData.currentSpot.get('id'),
						type : form.type,
						language: form.codelang,
						title: form.title,
						text: form.textContent
					    });
	    
	    Ext.data.StoreManager.lookup('SpotDescriptionsContent').add(newSpotDescription);
	    
	}
	
    },
    
    
    
    
    
    
    
    
 });